package org.androidpn.server.dao.hibernate;

import org.androidpn.server.dao.ChatMsgDao;
import org.androidpn.server.model.ChatMessage;
import org.androidpn.server.model.User;
import org.androidpn.server.service.ChatMsgNotFoundException;
import org.androidpn.server.service.UserNotFoundException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by huang on 2017/4/20 0020.
 */
public class ChatMsgDaoHibernate extends HibernateDaoSupport implements ChatMsgDao {
    public void saveChatMessage(ChatMessage chatMessage) {
        getHibernateTemplate().saveOrUpdate(chatMessage);
    }

    public List<ChatMessage> findChatMessageByToUser(String toUserId) {
        List<ChatMessage> lists = getHibernateTemplate().find("from ChatMessage where toUser=?",toUserId);
        if(lists != null && lists.size()>0){
            return lists;
        }
        return null;
    }

    public void deleteChatMessage(ChatMessage chatMessage) {
        getHibernateTemplate().delete(chatMessage);
    }

    public void deleteChatMessageByUUID(String uuid) {
        List<ChatMessage> chatMessages = getHibernateTemplate().find("from ChatMessage where uuid=?",uuid);
        if(chatMessages!=null&&chatMessages.size()>0){
            ChatMessage notification = chatMessages.get(0);
            deleteChatMessage(notification);
        }
    }

    public ChatMessage getChatMessageByUuid(String uuid) throws ChatMsgNotFoundException {
        List<ChatMessage> lists = getHibernateTemplate().find("from ChatMessage where uuid=?",uuid);
        if(lists.size()>0){
            return (ChatMessage) lists.get(0);
        }
        return null;
    }
}
